﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Dng.Application.Logging;
using ShippingManage.DataAccessor.Entities;
using ShippingManage.Models;

namespace ShippingManage
{
	public partial class frmAirWayBill : FormBase
	{
		public frmAirWayBill()
		{
			InitializeComponent();
		}
		private void frmAirWayBill_Load(object sender, EventArgs e)
		{
			//HouseAirWayBillModel model = new HouseAirWayBillModel();
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			//var data = new ShippingManageContainer();
			//var houseAirWayBill = from c in data.HouseAirWayBills
			//                      where c.Id==
		}

		HouseAirWayBill GetInfo()
		{
			var obj = new HouseAirWayBill();

			if (IdInput.Text.Trim().Length < 1)
				obj.Id = Guid.Empty;
			else
				obj.Id = new Guid(IdInput.Text);

			obj.Created = DateTime.Now;

			obj.Modified = DateTime.Now;

			obj.ShipperName = txtShipperName.Text;
			obj.ShipperAddress = txtShipperAddress.Text;
			obj.ShipperTelephone = txtShipperTel.Text;
			obj.ShipperFax = txtShipperFax.Text;

			obj.ConsigneeName = txtConsigneeName.Text;
			obj.ConsigneeAddress = txtConsigneeAddress.Text;
			obj.ConsigneeTelephone = txtConsigneeTel.Text;
			obj.ConsigneeFax = txtConsigneeFax.Text;

			obj.IssuingName = txtIssuingName.Text;
			obj.IssuingCity = txtIssuingCity.Text;
			obj.IssuingTelephone = txtIssuingTel.Text;
			obj.IssuingFax = txtIssuingFax.Text;

			obj.AirportDepature = txtAirportOfDeparture.Text;
			obj.To1 = txtTo1.Text;
			obj.To2 = txtTo2.Text;
			obj.By1 = txtBy1.Text;
			obj.By2 = txtBy2.Text;
			obj.AirportDestination = txtAirPortOfDestination.Text;
			obj.Flight1 = txtFlight1.Text;
			obj.Date1 = Convert.ToDateTime(txtDate1.Text);
			obj.Flight2 = txtFlight2.Text;
			obj.Date2 = Convert.ToDateTime(txtDate2.Text);

			obj.AccountName = txtAccountName.Text;
			obj.AccountAddress = txtAccountAddress.Text;
			obj.AccountTelephone = txtAccountingTel.Text;
			obj.AccountFax = txtAccountingFax.Text;
			obj.Currency = txtCurrency.Text;
			obj.Declared1 = txtDeclare1.Text;
			obj.Declared2 = txtDeclare2.Text;
			obj.Amount = Convert.ToDouble(txtAmountInsurance.Text);
			obj.Diversion = txtDeversion.Text;
			obj.DateExecute = Convert.ToDateTime(txtExecuteDate.Text);
			obj.Place = txtPlace.Text;
			obj.Signature = txtSignature.Text;

			obj.Code1 = txtCode1.Text.Trim();
			obj.Code2 = txtCode2.Text.Trim();

			obj.HAWBItems = new List<HAWBItem>();
			HAWBItem hawbItem;
			//collect order line item
			foreach (DataGridViewRow item in dataGridViewProducts.Rows)
			{
				if (item.IsNewRow) continue;

				hawbItem = new HAWBItem();
				if (item.Cells["Id"].Value == null || item.Cells["Id"].Value.ToString() == string.Empty)
				{ }
				else
					hawbItem.Id = (Guid)item.Cells["Id"].Value;

				if (item.Cells["NoPriceRPC"].Value != null)
					hawbItem.NoOfPricesRPC = (string)item.Cells["NoPriceRPC"].Value;
				if (item.Cells["GrossWeight"].Value != null)
					hawbItem.GrossWeight = (string)item.Cells["GrossWeight"].Value;
				if (item.Cells["RateClassCommodityItemNo"].Value != null)
					hawbItem.RateClassCommodityItemNo = (string)item.Cells["RateClassCommodityItemNo"].Value;
				if (item.Cells["ChargeableWeight"].Value != null)
					hawbItem.ChargeableWeight = (string)item.Cells["ChargeableWeight"].Value;
				if (item.Cells["RateCharge"].Value != null)
					hawbItem.RateCharge = (string)item.Cells["RateCharge"].Value;
				if (item.Cells["NatureAndQuantityOfGoods"].Value != null)
					hawbItem.NatureAndQuantityOfGoods = (string)item.Cells["NatureAndQuantityOfGoods"].Value;

				obj.HAWBItems.Add(hawbItem);
			}

			if (obj.HAWBItems.Count < 1)
				throw new ArgumentNullException("HAWBItems", "Not found any product");

			return obj;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			Save();
		}

		protected override void OnSave()
		{
			HouseAirWayBillModel model = new HouseAirWayBillModel();

			//try
			//{
			var obj = GetInfo();
			model.Save(obj);
			msg.Text = "Data saved";
			var temp = model.Get(obj.Id);
			txtCode3.Text = obj.Code3.ToString();
			//}
			//catch (Exception ex)
			//{
			//  msg.Text = ex.Message;
			//  Log.Error(ex, "Save HAWB failed");
			//}
		}

		#region Validate data
		private void txtShipperName_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtShipperName.Text.Length == 0)
			{
				error = "Please enter a name";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtShipperAddress_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtShipperAddress.Text.Length == 0)
			{
				error = "Please enter a address";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtShipperTel_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtShipperTel.Text.Length == 0)
			{
				error = "Please enter a telephone";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtShipperFax_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtShipperFax.Text.Length == 0)
			{
				error = "Please enter a Fax";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtConsigneeName_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtConsigneeName.Text.Length == 0)
			{
				error = "Please enter a name";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtConsigneeAddress_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtConsigneeAddress.Text.Length == 0)
			{
				error = "Please enter a address";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtConsigneeTel_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtConsigneeTel.Text.Length == 0)
			{
				error = "Please enter a telephone";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtConsigneeFax_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtConsigneeFax.Text.Length == 0)
			{
				error = "Please enter a fax";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtIssuingName_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtIssuingName.Text.Length == 0)
			{
				error = "Please enter a name";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}


		private void txtIssuingCity_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtIssuingCity.Text.Length == 0)
			{
				error = "Please enter a city";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtIssuingTel_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtIssuingTel.Text.Length == 0)
			{
				error = "Please enter a telephone";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtAirportOfDeparture_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtAirportOfDeparture.Text.Length == 0)
			{
				error = "Please enter a airport depature";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtTo1_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtTo1.Text.Length == 0)
			{
				error = "Please enter a to departure";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtBy1_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtBy1.Text.Length == 0)
			{
				error = "Please enter a to by first carrier";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}
		private void txtTo2_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtTo2.Text.Length == 0)
			{
				error = "Please enter a to departure";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtBy2_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtBy2.Text.Length == 0)
			{
				error = "Please enter a by carrier";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtAirPortOfDestination_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtAirPortOfDestination.Text.Length == 0)
			{
				error = "Please enter a airport destination";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtFlight1_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtFlight1.Text.Length == 0)
			{
				error = "Please enter a code flight";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtDate1_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtDate1.Text.Length == 0)
			{
				error = "Please enter a date";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtFlight2_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtFlight2.Text.Length == 0)
			{
				error = "Please enter a code flight";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtDate2_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtDate2.Text.Length == 0)
			{
				error = "Please enter a date";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtAirWayBill_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtAirWayBill.Text.Length == 0)
			{
				error = "Please enter a air way bill";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtIssuedBy_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtIssuedBy.Text.Length == 0)
			{
				error = "Please enter a issued";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtAccountName_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtAccountName.Text.Length == 0)
			{
				error = "Please enter a account name";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtAccountAddress_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtAccountAddress.Text.Length == 0)
			{
				error = "Please enter a account address";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtAccountingTel_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtAccountingTel.Text.Length == 0)
			{
				error = "Please enter a telephone";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtAccountingFax_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtAccountingFax.Text.Length == 0)
			{
				error = "Please enter a fax";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtCurrency_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtCurrency.Text.Length == 0)
			{
				error = "Please enter a currency";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtDeclare1_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtDeclare1.Text.Length == 0)
			{
				error = "Please enter a declare";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtDeclare2_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtDeclare2.Text.Length == 0)
			{
				error = "Please enter a declare";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtAmountInsurance_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtAmountInsurance.Text.Length == 0)
			{
				error = "Please enter a amount";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtDeversion_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtDeversion.Text.Length == 0)
			{
				error = "Please enter a deversion";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtExecuteDate_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtExecuteDate.Text.Length == 0)
			{
				error = "Please enter a execute date";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtPlace_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtPlace.Text.Length == 0)
			{
				error = "Please enter a place";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}

		private void txtSignature_Validating(object sender, CancelEventArgs e)
		{
			string error = null;
			if (txtSignature.Text.Length == 0)
			{
				error = "Please enter a signature";
				e.Cancel = true;
			}
			errorProvider1.SetError((Control)sender, error);
		}
		#endregion

		private void ObjectPreviewButton_Click(object sender, EventArgs e)
		{
			try
			{
				new ObjectPreviewForm(GetInfo()).Show();
				msg.Text = "Showed preview form";
			}
			catch (Exception ex)
			{
				msg.Text = ex.Message;
				Log.Error(ex, "Show preview form failed");
			}
		}

		protected override void OnNew()
		{
			IdInput.Text = Guid.Empty.ToString();

			txtShipperName.Text = "";
			txtShipperAddress.Text = "";
			txtShipperTel.Text = "";
			txtShipperFax.Text = "";

			txtConsigneeName.Text = "";
			txtConsigneeAddress.Text = "";
			txtConsigneeTel.Text = "";
			txtConsigneeFax.Text = "";

			txtIssuingName.Text = "";
			txtIssuingCity.Text = "";
			txtIssuingTel.Text = "";
			txtIssuingFax.Text = "";

			txtAirportOfDeparture.Text = "";
			txtTo1.Text = "";
			txtTo2.Text = "";
			txtBy1.Text = "";
			txtBy2.Text = "";
			txtAirPortOfDestination.Text = "";
			txtFlight1.Text = "";
			txtDate1.Text = "";
			txtFlight2.Text = "";
			txtDate2.Text = "";

			txtAccountName.Text = "";
			txtAccountAddress.Text = "";
			txtAccountingTel.Text = "";
			txtAccountingFax.Text = "";
			txtCurrency.Text = "";
			txtDeclare1.Text = "";
			txtDeclare2.Text = "";
			txtAmountInsurance.Text = "0";
			txtDeversion.Text = "";
			txtExecuteDate.Text = "";
			txtPlace.Text = "";
			txtSignature.Text = "";

			txtCode1.Text = "";
			txtCode2.Text = "";
			txtCode3.Text = "";
		}
	}
}


